package org.leviatan.definator.gui.swingcomponents;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;

import org.leviatan.definator.gui.RuntimeContext;
import org.leviatan.definator.gui.control.ListenerFactory;
import org.leviatan.definator.gui.swingmodels.DefinitionsTreeModel;

public class LeftMainPanel extends JPanel {
	
	private static final long serialVersionUID = 8305270159534201941L;

	private JToolBar tBar;
	
	private JTree jTree;
	
	private JButton butCreateTbl, butDelete, butCreateFld, butCreateRec;
	
	public LeftMainPanel() {
		super();
	
		this.setLayout(new GridBagLayout());
		
		//this.setPreferredSize(size);
	
		
		this.tBar = this.initToolBar();
		
		this.jTree = this.initJTree();
		RuntimeContext.jTree = this.jTree;
	
		
		GridBagConstraints tBarConstr = new GridBagConstraints();
		tBarConstr.anchor = GridBagConstraints.NORTHWEST;
		tBarConstr.fill = GridBagConstraints.BOTH;
		tBarConstr.gridx = 0;
		tBarConstr.gridy = 0;
		
		GridBagConstraints treeConstr = new GridBagConstraints();
		treeConstr.anchor = GridBagConstraints.NORTHWEST;
		treeConstr.insets = new Insets(5, 5, 5, 5);
		treeConstr.fill = GridBagConstraints.BOTH;
		treeConstr.gridx = 0;
		treeConstr.gridy = 1;
		treeConstr.weightx = 1.0;
		treeConstr.weighty = 1.0;
		
		this.add(this.jTree, treeConstr);
		this.add(this.tBar, tBarConstr);
		
		this.setComponentZOrder(tBar, 0);
		
		// add listeners to buttons
		
		ListenerFactory.initButCreateTblListener(butCreateTbl);
		ListenerFactory.initButCreateRecListener(butCreateRec);
		ListenerFactory.initButCreateFldListener(butCreateFld);
		ListenerFactory.initButDeleteListener(butDelete);
		
	}
	
	private JTree initJTree() {
		if (jTree == null ) {
			jTree = new JTree();
			jTree.setModel(new DefinitionsTreeModel());
			ListenerFactory.initTreeSelectionListener(jTree);
			//jTree.setPreferredSize(size);
		}
		
		return jTree;
	}
	
	

	
	public JToolBar initToolBar() {
		
		JToolBar aBar = new JToolBar("tBar");
		aBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butCreateTblConstr = new GridBagConstraints();
		butCreateTblConstr.anchor = GridBagConstraints.WEST;
		butCreateTblConstr.insets = new Insets(0, 5, 0, 5);
		butCreateTblConstr.gridx = 0;
		butCreateTblConstr.gridy = 0;
		butCreateTblConstr.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteConstr = new GridBagConstraints();
		butDeleteConstr.anchor = GridBagConstraints.WEST;
		butDeleteConstr.insets = new Insets(0, 0, 0, 5);
		butDeleteConstr.gridx = 3;
		butDeleteConstr.gridy = 0;
		butDeleteConstr.weightx = 1.0;
		butDeleteConstr.weighty = 1.0;
		
		GridBagConstraints butCreateRecConstr = new GridBagConstraints();
		butCreateRecConstr.anchor = GridBagConstraints.WEST;
		butCreateRecConstr.insets = new Insets(0, 0, 0, 5);
		butCreateRecConstr.gridx = 1;
		butCreateRecConstr.gridy = 0;
		
		GridBagConstraints butCreateFldConstr = new GridBagConstraints();
		butCreateFldConstr.anchor = GridBagConstraints.WEST;
		butCreateFldConstr.insets = new Insets(0, 0, 0, 5);
		butCreateFldConstr.gridx = 2;
		butCreateFldConstr.gridy = 0;
		
		butCreateTbl = new JButton("new table");
		butDelete = new JButton("delete");
		butCreateFld = new JButton("new field");
		butCreateRec = new JButton("new record");
		
		aBar.add(butCreateTbl, butCreateTblConstr);
		aBar.add(butDelete, butDeleteConstr);
		aBar.add(butCreateFld, butCreateFldConstr);
		aBar.add(butCreateRec, butCreateRecConstr);
		
		aBar.setRollover(true);
		aBar.setFloatable(false);
		
		return aBar;
		
	}

}
